package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func init() {
	DB = InitDB()
}

func InitDB() *gorm.DB {
	driverName := "mysql"
	host := "localhost"
	port := "3306"
	database := "library"
	username := "root"
	password := "123"
	charset := "utf8"
	args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
		username,password,host,port,database,charset)

	db, err := gorm.Open(mysql.New(mysql.Config{
		DriverName: driverName,
		DSN:        args, // Data Source Name
	}), &gorm.Config{})
	if err != nil {
		fmt.Printf("数据库连接失败,%v" , err)
		return nil
	}
	//db.AutoMigrate(&model.User{})
	//db.AutoMigrate(&model.UserBorrow{})
	//db.AutoMigrate(&model.Tag{})
	//db.AutoMigrate(&model.TagDesc{})
	//db.AutoMigrate(&model.Book{})
	//db.AutoMigrate(&model.BookDesc{})

	return db
}

func GetDB() *gorm.DB {
	return DB
}